<!DOCTYPE html>
<html lang="en">
<head>
    {{include file="lib/page_head" /}}

    <style>
        table th{word-wrap:break-word;word-break:break-all;font-size:12px!important;}
        table td{word-wrap:break-word;word-break:break-all;font-size:12px!important;}
        .from_table .td_l,.from_table .td_r{font-size:14px!important;line-height: 25px;word-wrap:break-word;word-break:break-all}
        #search_form .form-control,#search_form .select5{width:100px;height:28px;padding:5px;}
        #search_form .layui-input-inline{width:auto;margin: 0;}
        #search_form .form-group{margin-right:0;margin-bottom: 5px;}
        #search_form .btn{height:30px;line-height:30px;padding:0 10px;}
    </style>
</head>

<body>



<!-- Preloader -->
<div id="preloader">
    <div id="status"><i class="fa fa-spinner fa-spin"></i></div>
</div>

<section>
    {{include file="lib/page_left" /}}
    <div class="mainpanel">

        {{include file="lib/page_header" /}}

        <div class="contentpanel" >
            <div class="panel panel-default">
                <div class="panel-heading">
                    <div class="panel-btns">
                        <a href="" class="panel-close">×</a>
                        <a href="" class="minimize">−</a>
                    </div>
                    <h4 class="panel-title">企业列表</h4>
                </div>
                <div class="panel-body ">
                    <div id="info_list" class="form-inline">
                        <div class="btn-group">
                            <button class="btn btn-primary" onclick="edit_info(0)">添加</button>
                            <!--<button class="btn btn-primary" onclick="add_list()">批量添加</button>-->
                            <!--<button class="btn btn-primary" onclick="del_info(0)">删除</button>-->
                            <!--<button class="btn btn-primary" onclick="set_cancel(0)">取消</button>-->
                            <button class="btn btn-primary" onclick="location.href = location.href;">刷新</button>

                        </div>
                        <div class="layui-form-item search_tools" style="margin-top:10px;" >
                            <form class="layui-form" id="search_form" action="" onsubmit="return false;">
                                <div class="form-group">
                                    <label >企业名称</label>
                                    <input class="form-control" name="name"  autocomplete="off" type="text">
                                </div>
                                <div class="form-group">
                                    <label >代理</label>
                                    <select class="select5" name="agent_id" data-search="true">
                                        <option value="0">请选择</option>
                                        {{foreach $agent_list as $val }}
                                        <option value="{{$val.id}}">{{$val.agent_name}}({{$val.id}})</option>
                                        {{/foreach}}
                                    </select>
                                </div>
                                <div class="form-group">
                                    <label >身份证</label>
                                    <input class="form-control" name="card_id"  autocomplete="off" type="text">
                                </div>
                                <div  class="form-group">
                                    <label > 状态</label>
                                    <select class="select5" name="status">
                                        <option value="-1">状态</option>
                                        <option value="0">正常</option>
                                        <option value="1">禁用</option>
                                    </select>
                                </div>


                                <div style="display: inline-block;clear: both;">
                                    <button class="btn btn-primary" onclick="search()">查询</button>
                                    <button class="btn btn-default" onclick="search_reset()">取消</button>
                                </div>
                            </form>
                            <div class="well well-sm search_toogle"><span class="glyphicon glyphicon-chevron-down"></span></div>
                        </div>
                    </div>
                </div>
            </div>
        </div><!-- contentpanel -->

    </div><!-- mainpanel -->
    {{include file="lib/page_right" /}}
</section>

<div id="edit_info" style="display: none;">
    <div class="layui-form-item" style="margin:10px;">

        <form>
            <input type="hidden" name="id" value="0"/>

            <div  class="form-group">
                <label > 企业名称<span style="color:red;">*</span></label>
                <input class="form-control" placeholder="请输入企业名称" name="name" type="text">
            </div>
            <div  class="form-group">
                <label > 代理</label>
                <select class="select5" name="agent_id" data-ignore="true" data-search="true">
                    <option value="0">请选择</option>
                    {{foreach $agent_list as $val }}
                    <option value="{{$val.id}}">{{$val.agent_name}}({{$val.id}})</option>
                    {{/foreach}}
                </select>
            </div>
            <div  class="form-group">
                <label > 联系人<span style="color:red;">*</span></label>
                <input class="form-control" placeholder="请输入联系人" name="real_name" type="text">
            </div>
            <div  class="form-group">
                <label > 联系电话<span style="color:red;">*</span></label>
                <input class="form-control" placeholder="请输入联系电话" name="mobile" type="text">
            </div>
            <div  class="form-group">
                <label > 联系地址<span style="color:red;">*</span></label>
                <input class="form-control" placeholder="请输入联系地址" name="adress" type="text">
            </div>
            <div  class="form-group">
                <label > 法人名称<span style="color:red;">*</span></label>
                <input class="form-control" placeholder="请输入法人名称" name="legal_name" type="text">
            </div>
            <div  class="form-group">
                <label > 法人身份证<span style="color:red;">*</span></label>
                <input class="form-control" placeholder="请输入法人身份证" name="legal_id_card" type="text">
            </div>
            <div  class="form-group">
                <label > 营业执照编号<span style="color:red;">*</span></label>
                <input class="form-control" placeholder="请输入营业执照编号" name="business_no" type="text">
            </div>
            <div  class="form-group">
                <label > 开票信息编号<span style="color:red;">*</span></label>
                <input class="form-control" placeholder="请输入开票信息编号" name="billing_no" type="text">
            </div>
            <div  class="form-group">
                <label > 折扣<span style="color:red;">*</span></label>
                <input class="form-control" placeholder="请输入折扣" name="discount" type="text" value="0.94">
            </div>
            <div  class="form-group">
                <label > 代理佣金折扣<span style="color:red;">*</span></label>
                <input class="form-control" placeholder="请输入代理佣金折扣" name="rebate_discount" type="text"  value="0.01" >
            </div>
            <div  class="form-group">
                <label > 状态<span style="color:red;">*</span></label>
                <select class="select5" name="status">
                    <option value="0">正常</option>
                    <option value="1">禁用</option>
                </select>
            </div>

        </form>
    </div>
</div>
<div style="display: none;" id="set_credit">
    <form class="layui-form" action="">
        <input name="id" value="0" type="hidden">
        <div class="form-group">
            <label > 授信金额（正数增加，负数递减）<span style="color:red;">*</span></label>
            <input type="number" name="credit"  placeholder="授信金额" class="form-control" >
        </div>
    </form>
</div>
<div style="display: none;" id="set_balance">
    <form class="layui-form" action="">
        <input name="id" value="0" type="hidden">
        <div class="form-group">
            <label > 加款金额（正数增加，负数递减）<span style="color:red;">*</span></label>
            <input type="number" name="balance"  placeholder="加款金额" class="form-control" >
        </div>
    </form>
</div>
<script src="../js/jquery-1.11.1.min.js"></script>
<script src="../js/jquery-migrate-1.2.1.min.js"></script>
<script src="../js/jquery-ui-1.10.3.min.js"></script>
<script src="../js/bootstrap.min.js"></script>
<script src="../js/jquery.sparkline.min.js"></script>
<script src="../js/toggles.min.js"></script>
<script src="../js/jquery.cookies.js"></script>
<script src="../js/jquery.tmpl.js"></script>
<script src="../js/custom.js"></script>
<script src="../layui/layui.js"></script>
<script src="../js/jquery.gritter.min.js"></script>
<script src="../js/form.beautify.js?t={{$version}}"></script>
<script src="../js/commen.js?t={{$version}}"></script>
<script src="../js/datatables.js?t={{$version}}"></script>
<link href="../css/ley.dialog.css" rel="stylesheet">
<script src="../js/ley.dialog.js?t={{$version}}"></script>
<script>
    layui.use(['laydate','laypage',"element"], function(){

    });
    var admin_id = "{{$ADMIN_ID}}";

    $(function () {


        $("#info_list").datatables({
            idField: 'order_no',
            mobile_field:["ck","name","status"],
            columns:[
                {field:'ck',checkbox:true }
                ,{field:'id',width:50,  title: 'ID'}
                ,{field:'name',width:120,  title: '企业名称'}
                ,{field:'real_name',width:50,  title: '联系人'}
                ,{field:'mobile',width:80,  title: '联系电话'}
                ,{field:'legal_name',width:120,  title: '法人名称'} //minWidth：局部定义当前单元格的最小宽度，layui 2.2.1 新增
                ,{field:'agent_name',width:80,title: '代理人',formatter:function (value,row,index) {
                        if(row.agent_name)
                        {
                            return row.agent_name+"("+row.agent_id+")";
                        }else{
                            return "无";
                        }

                    }}
                ,{field:'balance',width:120, title: '余额',formatter:function(value,row,index){return (value * 0.0001).toFixed(2) + "";}}
                ,{field:'credit',width:120, title: '授信',formatter:function(value,row,index){return (value * 0.0001).toFixed(2) + "";}}
                ,{field:'discount',width:60, title: '折扣',formatter:function(value,row,index){return (value * 0.0001).toFixed(2) + "";}}
                ,{field:'rebate_discount',width:150,  title: '代理佣金折扣',formatter:function(value,row,index){return (value * 0.0001).toFixed(2) + "";}}
                ,{field:'status',width:60,  title: '状态',formatter:function(value,row,index){
                        if(value == 1)
                        {
                            return "禁用";
                        }else{
                            return "正常";
                        }
                    }}
                ,{field:'ctime', width:170,  title: '创建时间', formatter: function (value, row, index) {return getLocalTime(value) + "";}}
                ,{field:'operation',title:'操作',width:30,formatter:function (value,row,index) {
                        var text = "";
                        text += '<div class="btn-group"> ';
                        text += '<a data-toggle="dropdown" class="dropdown-toggle"> ';
                        text += '<i class="fa fa-cog"></i> ';
                        text += '</a> ';
                        text += '<ul role="menu" class="dropdown-menu pull-right"> ';

                        text +='<li><a href="javascript:void 0;" onclick="edit_info(\''+row.id+'\')">编辑</a></li> ' ;
                        text +='<li><a href="javascript:void 0;" onclick="set_credit(\''+row.id+'\')">授信</a></li> ' ;
                        text +='<li><a href="javascript:void 0;" onclick="set_balance(\''+row.id+'\')">加款</a></li> ' ;
                        text +='</ul> ';
                        text +='</div>';
                        return text;
                    }}

            ],
            ajax:{
                url:"/companys/ajax_company_list",
                parems:get_search_data(),
                callback:function (msg) {
                    check_table_list_data(msg);
                    check_parent_height();
                }
            }

        });
    });
    function add_balance(id)
    {
        $("#add_balance form")[0].reset();
        $("#add_balance").find("input[name='id']").val(id);
        openDialog2({title:"增加预存款",area: ['600', "400"],content:$("#add_balance"),clone:true,before:function (layero) {

            },
            yes:function (index,layero) {
                var data = {};
                var is_false = false;

                $("input[type!='radio'],select,textarea,input[type='radio']:checked",layero).each(function () {
                    var name = $(this).attr("name");

                    if(name)
                    {
                        var value = $(this).val();
                        var ignore = $(this).attr("data-ignore");
                        if(value)
                        {
                            data[name] = value;
                            $(this).parents(".form-group").removeClass("has-error");
                        }else{
                            if (ignore != "true")
                            {
                                $(this).parents(".form-group").addClass("has-error");
                                is_false = true;
                            }

                        }


                    }
                });
                if(is_false)
                {
                    return false;
                }
                AjaxCommen("GET","/card/ajax_add_balance",data,function (msg) {
                    if(msg.error === 0)
                    {
                        openMsg("成功");
                        layero.close();
                        search();
                    }
                });
            }
        });
    }

    function edit_info(id) {
        $("#edit_info form")[0].reset();
        $("#edit_info #product_list").empty();
        $("#edit_info").find("input[name='id']").val(0);
        $("#edit_info").find("input[name='card_no']").val("");
        $("#edit_info").find("select[name='status']").val(0).trigger("change");

        if(id)
        {
            AjaxCommen("GET","/companys/ajax_company_info",{id:id},function (msg) {
                if (!msg.error)
                {
                    $("#edit_info input[type!='radio'],#edit_info select,#edit_info textarea").each(function () {
                        var name = $(this).attr("name");

                        if(name&&msg.data[name]!==undefined)
                        {
                            $(this).val(msg.data[name]);

                        }
                        if($(this).is("select"))
                        {
                            $(this).trigger("change");
                        }
                    });
                    if(msg.data.status != 0)
                    {
                        $("#edit_info select[name='status']")[0].disabled = true;
                        $("#edit_info select[name='status']").trigger("change");

                    }
                    $("#edit_info input[name='id']").val(msg.data.id);


                    open_info();
                }
            });
        }else{

            open_info();
        }
    }
    function open_info()
    {
        $("#edit_info select").trigger("change");
        openDialog2({title:"加油卡信息",area: ['400', "600"],content:$("#edit_info"),clone:true,before:function (layero) {},
            yes:function (index,layero) {
                var data = {};
                var is_false = false;

                $("#edit_info input[type!='radio'],#edit_info select,#edit_info textarea,#edit_info input[type='radio']:checked",layero).each(function () {
                    var name = $(this).attr("name");

                    if(name)
                    {
                        var value = $(this).val();
                        var ignore = $(this).attr("data-ignore");
                        console.log(name,value);
                        if(value)
                        {
                            data[name] = value;
                            $(this).parents(".form-group").removeClass("has-error");
                        }else{
                            if (ignore != "true")
                            {
                                $(this).parents(".form-group").addClass("has-error");
                                is_false = true;
                            }

                        }


                    }
                });
                if(is_false)
                {
                    return false;
                }
                AjaxCommen("GET","/companys/ajax_company_info_save",data,function (msg) {
                    if(msg.error === 0)
                    {
                        openMsg("成功");
                        layero.close();
                        search();
                    }
                });
            }
        });
    }

    function set_credit(order_id)
    {
        $("#set_credit form")[0].reset();
        $("#set_credit input[name='id']").val(order_id);
        openDialog2({title:"授信信息",area: ['400', "200"],content:$("#set_credit"),clone:true,before:function (layero) {

            },
            yes:function (index,layero) {
                var data = {};
                var is_false = false;

                $("input[type!='radio'],select,textarea,input[type='radio']:checked",layero).each(function () {
                    var name = $(this).attr("name");
                    var ignore = $(this).attr("data-ignore");
                    if(name)
                    {
                        var value = $(this).val();

                        if(value)
                        {
                            data[name] = value;
                            $(this).parents(".form-group").removeClass("has-error");
                        }else{
                            if (ignore != "true")
                            {
                                $(this).parents(".form-group").addClass("has-error");
                                is_false = true;
                            }
                        }
                    }
                });
                if(is_false)
                {
                    return false;
                }
                AjaxCommen("GET","/companys/ajax_company_set_credit",data,function (msg) {
                    if(msg.error === 0)
                    {
                        openMsg("成功");
                        layero.close();
                        search();
                    }
                });
            }
        });
    }
    function set_balance(order_id)
    {
        $("#set_balance form")[0].reset();
        $("#set_balance input[name='id']").val(order_id);
        openDialog2({title:"加款信息",area: ['400', "200"],content:$("#set_balance"),clone:true,before:function (layero) {

            },
            yes:function (index,layero) {
                var data = {};
                var is_false = false;

                $("input[type!='radio'],select,textarea,input[type='radio']:checked",layero).each(function () {
                    var name = $(this).attr("name");
                    var ignore = $(this).attr("data-ignore");
                    if(name)
                    {
                        var value = $(this).val();

                        if(value)
                        {
                            data[name] = value;
                            $(this).parents(".form-group").removeClass("has-error");
                        }else{
                            if (ignore != "true")
                            {
                                $(this).parents(".form-group").addClass("has-error");
                                is_false = true;
                            }
                        }
                    }
                });
                if(is_false)
                {
                    return false;
                }
                AjaxCommen("GET","/companys/ajax_company_set_balance",data,function (msg) {
                    if(msg.error === 0)
                    {
                        openMsg("成功");
                        layero.close();
                        search();
                    }
                });
            }
        });
    }
    function search()
    {
        var data = get_search_data();
        console.log(data);
        $("#info_list").setParems(data);
        $("#info_list").reload();

    }
    function search_reset() {
        $("#search_form")[0].reset();
        search();
    }
    function get_search_data() {
        var data = {};
        $('#search_form select,#search_form input').each(function () {
            var name = $(this).attr("name");
            if(name)
            {
                data[name] = $(this).val();
            }

        });
        return data;
    }
</script>

</body>
</html>
